```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java字符串常量池深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 700;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #2d2d2d;
            border-radius: 8px;
            color: #f8f8f2;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            position: relative;
        }
        .highlight::before {
            content: "";
            position: absolute;
            left: -4px;
            top: 0;
            bottom: 0;
            width: 4px;
            background: linear-gradient(to bottom, #667eea, #764ba2);
            border-radius: 2px;
        }
        .footer {
            background-color: #1a202c;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 md:py-32 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-6xl font-bold mb-6 animate-fadeIn">Java字符串常量池深度解析</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8 max-w-3xl mx-auto">探索JVM中字符串存储的秘密 - 从编译时优化到运行时机制</p>
            <div class="flex justify-center space-x-4">
                <a href="#overview" class="px-6 py-3 bg-white text-indigo-700 font-semibold rounded-lg hover:bg-opacity-90 transition-colors">
                    <i class="fas fa-book-open mr-2"></i>开始阅读
                </a>
                <a href="#diagram" class="px-6 py-3 bg-indigo-800 bg-opacity-50 text-white font-semibold rounded-lg hover:bg-opacity-70 transition-colors">
                    <i class="fas fa-project-diagram mr-2"></i>关系图示
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Overview Section -->
        <section id="overview" class="mb-16">
            <div class="bg-white rounded-xl p-8 shadow-md">
                <h2 class="text-3xl font-bold mb-6 text-gray-800 flex items-center">
                    <i class="fas fa-info-circle text-indigo-600 mr-3"></i>
                    字符串常量池概述
                </h2>
                <p class="text-lg text-gray-700 mb-6">
                    在Java中，字符串常量池（String Constant Pool）是一个特殊的内存区域，用于存储字符串字面值（string literals）和通过特定方式创建的字符串。它是JVM方法区的一部分，主要目的是优化字符串存储，避免创建重复的字符串对象。
                </p>
                
                <div class="grid md:grid-cols-2 gap-6 mt-8">
                    <div class="card bg-gradient-to-br from-blue-50 to-indigo-50 p-6 rounded-lg">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-3 rounded-full mr-4">
                                <i class="fas fa-magic text-indigo-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold text-gray-800">主要优势</h3>
                        </div>
                        <ul class="space-y-2 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>节省内存空间，避免重复字符串</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>提高性能，减少对象创建开销</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>保证字符串不可变性</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="card bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-lg">
                        <div class="flex items-center mb-4">
                            <div class="bg-pink-100 p-3 rounded-full mr-4">
                                <i class="fas fa-exclamation-triangle text-pink-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold text-gray-800">注意事项</h3>
                        </div>
                        <ul class="space-y-2 text-gray-700">
                            <li class="flex items-start">
                                <i class="fas fa-exclamation text-yellow-500 mt-1 mr-2"></i>
                                <span>不要过度使用intern()方法</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-exclamation text-yellow-500 mt-1 mr-2"></i>
                                <span>理解编译时与运行时的区别</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-exclamation text-yellow-500 mt-1 mr-2"></i>
                                <span>注意字符串拼接的性能影响</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Content Sections -->
        <section class="space-y-12">
            <!-- Section 1 -->
            <div class="bg-white rounded-xl p-8 shadow-md highlight">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">1</span>
                    编译时字面值创建的字符串
                </h2>
                
                <div class="mb-6">
                    <h3 class="text-xl font-semibold mb-3 text-gray-700 flex items-center">
                        <i class="fas fa-code text-indigo-500 mr-2"></i>
                        编译时加入常量池
                    </h3>
                    <p class="text-gray-700 mb-4">
                        在编译阶段，所有使用字面值形式创建的字符串都会自动放入字符串常量池。例如：
                    </p>
                    <div class="code-block p-4 mb-4 font-mono text-sm overflow-x-auto">
<pre><code class="text-white">String str1 = "Hello";
String str2 = "World";</code></pre>
                    </div>
                    <p class="text-gray-700">
                        在编译时，<code class="bg-gray-200 px-1 py-0.5 rounded">"Hello"</code> 和 <code class="bg-gray-200 px-1 py-0.5 rounded">"World"</code> 这两个字符串会被放入常量池中。
                    </p>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-700 flex items-center">
                        <i class="fas fa-share-alt text-indigo-500 mr-2"></i>
                        相同字面值共享
                    </h3>
                    <p class="text-gray-700 mb-4">
                        如果在同一类或不同类中使用了相同的字符串字面值，JVM 不会在常量池中创建多个副本，而是共享同一个字符串对象。例如：
                    </p>
                    <div class="code-block p-4 mb-4 font-mono text-sm overflow-x-auto">
<pre><code class="text-white">String str1 = "Hello";
String str2 = "Hello";</code></pre>
                    </div>
                    <p class="text-gray-700">
                        这里的 <code class="bg-gray-200 px-1 py-0.5 rounded">str1</code> 和 <code class="bg-gray-200 px-1 py-0.5 rounded">str2</code> 都会指向常量池中的同一个 <code class="bg-gray-200 px-1 py-0.5 rounded">"Hello"</code> 对象。
                    </p>
                </div>
            </div>

            <!-- Section 2 -->
            <div class="bg-white rounded-xl p-8 shadow-md highlight">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">2</span>
                    使用 intern() 方法
                </h2>
                
                <div class="mb-6">
                    <h3 class="text-xl font-semibold mb-3 text-gray-700 flex items-center">
                        <i class="fas fa-hand-point-right text-indigo-500 mr-2"></i>
                        手动加入常量池
                    </h3>
                    <p class="text-gray-700 mb-4">
                        当通过 <code class="bg-gray-200 px-1 py-0.5 rounded">new</code> 关键字或其他方式创建字符串时，该字符串最初存储在堆内存中，而不是常量池中。如果希望将此字符串放入常量池中，可以调用 <code class="bg-gray-200 px-1 py-0.5 rounded">intern()</code> 方法。<code class="bg-gray-200 px-1 py-0.5 rounded">intern()</code> 方法会先检查常量池中是否存在相同内容的字符串：
                    </p>
                    <div class="code-block p-4 mb-4 font-mono text-sm overflow-x-auto">
<pre><code class="text-white">String str1 = new String("Hello");
String str2 = str1.intern();</code></pre>
                    </div>
                    <p class="text-gray-700">
                        在这个例子中，<code class="bg-gray-200 px-1 py-0.5 rounded">str1</code> 最初指向堆中的 <code class="bg-gray-200 px-1 py-0.5 rounded">"Hello"</code> 对象，调用 <code class="bg-gray-200 px-1 py-0.5 rounded">str1.intern()</code> 后，如果 <code class="bg-gray-200 px-1 py-0.5 rounded">"Hello"</code> 不在常量池中，它将被加入常量池，并返回常量池中的引用。
                    </p>
                </div>
                
                <div class="grid md:grid-cols-2 gap-6 mt-6">
                    <div class="bg-blue-50 p-4 rounded-lg">
                        <h4 class="font-semibold text-blue-800 mb-2 flex items-center">
                            <i class="fas fa-check-circle text-blue-500 mr-2"></i>
                            存在的情况
                        </h4>
                        <p class="text-gray-700">
                            如果常量池中已经存在相同内容的字符串，则直接返回池中的字符串引用。
                        </p>
                    </div>
                    <div class="bg-green-50 p-4 rounded-lg">
                        <h4 class="font-semibold text-green-800 mb-2 flex items-center">
                            <i class="fas fa-plus-circle text-green-500 mr-2"></i>
                            不存在的情况
                        </h4>
                        <p class="text-gray-700">
                            如果常量池中不存在，则将该字符串添加到常量池中，并返回其引用。
                        </p>
                    </div>
                </div>
            </div>

            <!-- Section 3 -->
            <div class="bg-white rounded-xl p-8 shadow-md highlight">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">3</span>
                    字符串常量表达式
                </h2>
                
                <div class="mb-6">
                    <h3 class="text-xl font-semibold mb-3 text-gray-700 flex items-center">
                        <i class="fas fa-link text-indigo-500 mr-2"></i>
                        编译时连接
                    </h3>
                    <p class="text-gray-700 mb-4">
                        当两个或多个字符串字面值通过 <code class="bg-gray-200 px-1 py-0.5 rounded">+</code> 操作符连接时，编译器会将它们的结果作为一个新的字符串常量，并直接放入常量池中。例如：
                    </p>
                    <div class="code-block p-4 mb-4 font-mono text-sm overflow-x-auto">
<pre><code class="text-white">String str = "Hello" + "World";</code></pre>
                    </div>
                    <p class="text-gray-700">
                        编译器会在编译时优化这个表达式，将 <code class="bg-gray-200 px-1 py-0.5 rounded">"HelloWorld"</code> 直接放入常量池。
                    </p>
                </div>
                
                <div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-700 flex items-center">
                        <i class="fas fa-random text-indigo-500 mr-2"></i>
                        运行时连接
                    </h3>
                    <p class="text-gray-700 mb-4">
                        如果涉及到变量或运行时计算的表达式，结果不会自动进入常量池，除非调用 <code class="bg-gray-200 px-1 py-0.5 rounded">intern()</code> 方法。例如：
                    </p>
                    <div class="code-block p-4 mb-4 font-mono text-sm overflow-x-auto">
<pre><code class="text-white">String str1 = "Hello";
String str2 = str1 + "World";  // 运行时计算，不会自动进入常量池
String str3 = str2.intern();   // 显式地加入常量池</code></pre>
                    </div>
                </div>
            </div>

            <!-- Section 4 -->
            <div class="bg-white rounded-xl p-8 shadow-md highlight">
                <h2 class="text-2xl font-bold mb-6 text-gray-800 flex items-center">
                    <span class="bg-indigo-600 text-white rounded-full w-8 h-8 flex items-center justify-center mr-3">4</span>
                    静态初始化
                </h2>
                
                <div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-700 flex items-center">
                        <i class="fas fa-cog text-indigo-500 mr-2"></i>
                        类加载时加入
                    </h3>
                    <p class="text-gray-700 mb-4">
                        在类加载过程中，通过静态初始化块中定义的字符串字面值也会进入常量池。例如：
                    </p>
                    <div class="code-block p-4 mb-4 font-mono text-sm overflow-x-auto">
<pre><code class="text-white">public class Test {
    static String str = "Hello";
}</code></pre>
                    </div>
                    <p class="text-gray-700">
                        在类加载时，<code class="bg-gray-200 px-1 py-0.5 rounded">"Hello"</code> 字符串会自动加入常量池。
                    </p>
                </div>
            </div>
        </section>

        <!-- Diagram Section -->
        <section id="diagram" class="mt-20">
            <div class="bg-white rounded-xl p-8 shadow-md">
                <h2 class="text-3xl font-bold mb-8 text-gray-800 flex items-center justify-center">
                    <i class="fas fa-project-diagram text-indigo-600 mr-3"></i>
                    字符串常量池工作机制图示
                </h2>
                
                <div class="mermaid text-center">
                    flowchart TD
                    A[字符串字面值] -->|编译时| B(字符串常量池)
                    C[new String] -->|堆内存| D(字符串对象)
                    D -->|intern()调用| B
                    E[字符串常量表达式] -->|编译时优化| B
                    F[静态初始化] -->|类加载时| B
                    G[运行时拼接] -->|堆内存| H(字符串对象)
                    H -->|可手动intern| B
                    
                    style B fill:#e3f2fd,stroke:#2196f3
                    style D fill:#ffebee,stroke:#f44336
                    style H fill:#ffebee,stroke:#f44336
                </div>
                
                <div class="mt-8 grid md:grid-cols-2 gap-6">
                    <div class="p-4 border border-indigo-200 rounded-lg bg-indigo-50">
                        <h3 class="text-lg font-semibold text-indigo-800 mb-3 flex items-center">
                            <i class="fas fa-database text-indigo-600 mr-2"></i>
                            字符串常量池特点
                        </h3>
                        <ul class="text-gray-700 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                <span>位于方法区（JDK7前）或堆内存（JDK7+）</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                <span>存储唯一的字符串对象</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check text-indigo-500 mt-1 mr-2"></i>
                                <span>自动管理，通过哈希表实现</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="p-4 border border-purple-200 rounded-lg bg-purple-50">
                        <h3 class="text-lg font-semibold text-purple-800 mb-3 flex items-center">
                            <i class="fas fa-lightbulb text-purple-600 mr-2"></i>
                            最佳实践建议
                        </h3>
                        <ul class="text-gray-700 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-star text-purple-500 mt-1 mr-2"></i>
                                <span>优先使用字面值而非new String()</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-star text-purple-500 mt-1 mr-2"></i>
                                <span>谨慎使用intern()，避免过度使用</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-star text-purple-500 mt-1 mr-2"></i>
                                <span>考虑使用StringBuilder处理复杂拼接</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="footer text-white py-8 mt-16">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-semibold mb-2">技术小馆</h3>
                    <p class="text-gray-400">深入研究Java核心技术</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-700 mt-6 pt-6 text-center text-gray-400 text-sm">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```